(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "react"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    const React = require("react");
    class DataList extends React.Component {
        constructor(props) {
            super(props);
            this.pageIndex = 0;
            this.state = { items: [] };
            this.loadData();
        }
        loadData() {
            if (this.status == 'complted' || this.status == 'loading') {
                return;
            }
            this.status = 'loading';
            this.props.loadData(this.pageIndex).then(items => {
                this.status = 'finish';
                if (items.length < this.props.pageSize)
                    this.status = 'complted';
                this.pageIndex = this.pageIndex + 1;
                // this.state.items = this.state.items.concat(items);
                // this.setState(this.state);
                let dataItems = this.state.items;
                dataItems.push(...items);
                this.setState({ items: dataItems });
            }).catch(() => {
                this.status = 'fail';
            });
        }
        reset() {
            this.pageIndex = 0;
            this.status = null;
            // this.state.items = [];
            // this.setState(this.state);
            this.setState({ items: [] });
        }
        componentDidMount() {
            // let isEmpty = this.state.items.length == 0;
            // if (isEmpty)
            //     return;
            let scroller;
            if (this.props.scroller)
                scroller = this.props.scroller();
            if (scroller == null) {
                scroller = this.element.parentElement;
            }
            scrollOnBottom(scroller, this.loadData.bind(this));
        }
        createDataItem(data, index) {
            try {
                return this.props.dataItem(data, index);
            }
            catch (e) {
                let error = e;
                return React.createElement("div", null, error.message);
            }
        }
        render() {
            let indicator;
            switch (this.status) {
                case 'complted':
                    indicator = this.props.showCompleteText ?
                        React.createElement("div", null,
                            React.createElement("span", null, "\u6570\u636E\u5DF2\u5168\u90E8\u52A0\u8F7D\u5B8C"))
                        :
                            null;
                    break;
                case 'fail':
                    indicator =
                        React.createElement("button", { className: "btn btn-default btn-block", onClick: this.loadData }, "\u70B9\u51FB\u52A0\u8F7D\u6570\u636E");
                    break;
                default:
                    indicator =
                        React.createElement("div", null,
                            React.createElement("i", { className: "icon-spinner icon-spin" }),
                            React.createElement("span", null, "\u6570\u636E\u6B63\u5728\u52A0\u8F7D\u4E2D..."));
                    break;
            }
            return React.createElement("div", { ref: (o) => this.element = o, className: this.props.className },
                this.state.items.map((o, i) => this.createDataItem(o, i)),
                this.props.emptyItem != null && this.state.items.length == 0 ?
                    this.props.emptyItem
                    :
                        React.createElement("div", { className: "data-loading col-xs-12" }, indicator));
        }
    }
    exports.DataList = DataList;
    DataList.defaultProps = { pageSize: 10 };
    // let dataListDefaultProps: DataListProps = {} as DataListProps;
    // dataListDefaultProps.pageSize = 10;
    // DataList.defaultProps = dataListDefaultProps;
    /**
     * 滚动到底部触发回调事件
     */
    function scrollOnBottom(element, callback, deltaHeight) {
        console.assert(element != null);
        console.assert(callback != null);
        deltaHeight = deltaHeight || 10;
        let preScrollTop;
        element.addEventListener('scroll', function () {
            let maxScrollTop = element.scrollHeight - element.clientHeight;
            //let deltaHeight = 10;
            // if(element.scrollTop)
            //===============================================
            // 页面向下,无需理会
            if (element.scrollTop <= preScrollTop) {
                return;
            }
            //===============================================
            preScrollTop = element.scrollTop;
            if (element.scrollTop + deltaHeight >= maxScrollTop) {
                callback();
            }
        });
    }
});
